<?php 
include_once('includes.php'); 	

$sort="";
if(isset($_GET['sort']))
	$sort = $_GET['sort'];
$where=" where 1 ";
if($sort=="")
	$sort ="wait"; 
if($sort == "wait")
	$where.= " and status = '0'";
if($sort == "playing")
	$where.= " and status = '1'";
if($sort == "end")
	$where.= " and status = '2'";

//paging--------------
include_once('../includes/paging.class.php');

$q_str = $_SERVER['QUERY_STRING'];
if(isset($_GET['page']))
{
  $q_str = str_replace('page='.$_GET['page'],'',$_SERVER['QUERY_STRING']);    
}
$sql = "select count(*) as total from matchs left join competition on matchs.competition_id = competition.id $where ";
$r=db_fetch_one($sql);
$total=$r->total;

$paging = new Paging();

$paging->assign ( 'match?'.$q_str ,  $total , 20 );
$limit = ' Limit '.$paging->sql_limit();

//-------------------

$sql = "select matchs.*, competition.name as cname,competition.classicon as cicon  from matchs left join competition on matchs.competition_id = competition.id $where order by status asc ,kick_off desc $limit";
$matchs=db_fetch_all($sql);
$status = array("Wait","Playing","End");

?>
<?php include_once('includes/header.php'); 	 ?>
<span class="title">Match</span>

<a href="create-match" title="Create new match">Create new match</a>

<div style="padding:10px 0"> 
		Sort by match status: 
		<select name="sort" onchange="window.location='?sort='+this.value">
			<option value="wait" <?php echo ($sort=="wait")?"selected":""; ?> >wait</option>
			<option value="playing" <?php echo ($sort=="playing")?"selected":""; ?> >playing</option>
			<option value="end" <?php echo ($sort=="end")?"selected":""; ?> >end</option>
			
		</select> 
	</div>

<table> 
	<thead>
		<tr>
			<th> Competition </th>
			<th> Versus</th>
			<th> Kick off </th>
			<th> Status</th>
			<th> Total channel</th>
			<th> </th>
		</tr>
	</thead>
	
<?php 
foreach($matchs as $match)
{
	$sql = "select count(*) as total from channel where match_id= ". $match->id;
	$channel=db_fetch_one($sql);
	$local_kick_off = convertTimeToLocal($match->kick_off,$match->timezone);
?>
	<tr>
		<td class="<?php echo $match->cicon; ?>"><?php echo $match->cname;?></td>
		<td <?php echo ($match->is_feature)?"class='feature-match'":"";?>><?php echo $match->versus;?></td>
		<td><?php echo formatOutputDate($local_kick_off);?></td>
		<td class="match-status-<?php echo $status[$match->status];?>" ><?php echo $status[$match->status];?></td>
		<td><strong style="font-size:<?php echo $channel->total+12;?>px"><?php echo $channel->total;?></strong></td>
		<td>
			<?php if($match->status==2){ ?><a href="create-highlight?match_id=<?php echo $match->id; ?>">Create Highlight</a> | <?php } ?>
			<a href="create-match?id=<?php echo $match->id; ?>">Edit</a> |
			<a onclick="if(!confirm('Do you want to delete this match?')) return false;" href="create-match?task=delete&id=<?php echo $match->id; ?>">Delete</a>
		</td>
	</tr>
<?php
}
?>
</table>
<div class="pager">
<?php
	echo $paging->fetch();
?>
</div>

<?php include_once('includes/footer.php'); 	 ?>